﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace FierceWar
{
	public class LoadingScreen : Screen
	{
		
		public enum BackgroundImageMode
		{
			None,
			LoadPlayingScreen,
			LoadCreateMapScreen,
			LoadEditMapScreen,
		}

		public BackgroundImageMode BackgroundImage = BackgroundImageMode.None;

		// Progress bar.
		public ProgressBar LoadingBar;
		private float _loadingSlice;

		// Duration to show the screen.
		private float _duration;
		public float Duration
		{
			get
			{
				return _duration;
			}
			set
			{
				_duration = value;
				_stopwatch = _duration;
				
				_loadingSlice = 
					(LoadingBar.Maximum - LoadingBar.Value) /
					(_duration * 60 / 1000.0f);
			}
		}

		// Just stopwatch to determine elapsed time.
		private float _stopwatch;

		// Message to show.
		public TypingMessage Message;

		// Next screen.
		public Screen NextScreen;

		public LoadingScreen(
			GameResources resources, EventHandler screenEvent,
			string message, float duration, float timeToNextLetter)
			: base(resources, screenEvent)
		{
			int width = 800;
			int height = 15;
			LoadingBar = new ProgressBar(resources,
				new Vector2(
					(Resources.GraphicsDevice.Viewport.Width - width) / 2,
					(Resources.GraphicsDevice.Viewport.Height - height) / 2 + 250),
				0.0f, 100.0f, 0.0f, width, height, 3, 3, Color.White * 0.2f,
				Color.Transparent, Color.SteelBlue);

			Duration = duration;

			Message = new TypingMessage(resources, resources.LoadingFont, message, Color.White);
			Message.TimeToNextLetter = timeToNextLetter;
		}

		public void ResetLoadingBarColor()
		{
			LoadingBar.BorderColor = Color.White * 0.2f;
			LoadingBar.BackgroundColor = Color.Transparent;
			LoadingBar.ForegroundColor = Color.SteelBlue;
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);

			_stopwatch -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
			if (_stopwatch < 0.0f)
			{
				_stopwatch = _duration;
				Message.Reset();
				
				LoadingBar.Value = LoadingBar.Minimum;
				BackgroundImage = BackgroundImageMode.None;
				ScreenEvent.Invoke(this, new EventArgs());
			}
			else
			{
				Message.Update(gameTime);

				
				LoadingBar.Value += _loadingSlice;
				LoadingBar.Update();
			}
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);

			
			if (BackgroundImage == BackgroundImageMode.LoadPlayingScreen)
			{
				Resources.SpriteBatch.Draw(Resources.LoadPlayingScreen,
					FullScreen, Color.White);
			}
			else if (BackgroundImage == BackgroundImageMode.LoadCreateMapScreen)
			{
				Resources.SpriteBatch.Draw(Resources.LoadCreateMapScreen,
					FullScreen, Color.White);
			}
			else if (BackgroundImage == BackgroundImageMode.LoadEditMapScreen)
			{
				Resources.SpriteBatch.Draw(Resources.LoadEditMapScreen,
					FullScreen, Color.White);
			}

			Message.Draw();

			LoadingBar.Draw();
		}
	}
}
